import Vue from 'vue';
import VueRouter from 'vue-router';

import auth from './util/auth';

import Login from './view/login/login.vue';
import Home from './view/home/main.vue';
import Manager from './view/manager/main.vue';
import WoaManagerMain from './view/wechat-offical-account/main.vue';
import WoaManagerList from './view//wechat-offical-account/list.vue';
import WoaAddPage from './view/wechat-offical-account/detail.vue';
import PushMain from './view/message/main.vue';
import TemplateMain from './view/message/template/main.vue';
import TemplateList from './view/message/template/list.vue';
import TemplateDetail from './view/message/template/detail.vue';
import CustomerMain from './view/message/customer/main.vue';
import CustomerList from './view/message/customer/list.vue';
import CustomerDetail from './view/message/customer/detail.vue';
import SubscribeMain from './view/message/subscribe/main.vue';
import SubscribeList from './view/message/subscribe/list.vue';
import SubscribeDetail from './view/message/subscribe/detail.vue';
import SetPassword from './view/set-password/set-password.vue';
import UserMainPage from './view/user/main.vue';
import UserDetail from './view/user/detail.vue';
import UserList from './view/user/list.vue';
import GroupCollectionManager from './view/group-collection/gourpCollectionMain.vue';
import GroupManager from './view/group/main.vue';
import count from './view/count/main.vue';
import propellingMovementNum from './view/count/charts/propellingMovementNum.vue';
import countList from './view/count/table/list.vue';
import userPhoneManage from './view/count/userPhone/userPhoneManage.vue';
import dayPlay from './view/count/dayPlay/dayPlayIndex.vue';
import mouthPlay from './view/count/mouthPlay/mouthPlayIndex.vue';
import sevenKeepPercent from './view/count/sevenKeepPercent/index.vue';
import blackList from './view/count/blackList/index.vue';
import whiteList from './view/count/whiteList/index.vue';
import callbackAddress from './view/count/callbackAddress/index.vue';
import blackTacticsList from './view/count/blackTacticsList/index.vue';
// import GroupList from './view/group/groupList.vue';
// import GroupEdit from './view/group/groupEdit.vue';

const pulicRoutePaths = ['/', '/setPassword'];

const TYPE_IS_ADD = 'add';
const TYPE_IS_EDIT = 'edit';

const routes = [
  {
    path: '/',
    title: '登录',
    name: 'login',
    component: r => require.ensure([], () => r(Login), 'login'),
  },
  {
    path: '/setPassword',
    title: '更改密码',
    name: 'setPasswordPage',
    component: r => require.ensure([], () => r(SetPassword), 'setPasswordPage'),
  },
  {
    path: '/manager',
    title: '管理员管理页面',
    name: 'managerPage',
    component: r => require.ensure([], () => r(Manager), 'managerPage'),
  },
  {
    path: '/home',
    title: '管理页面',
    name: 'homePage',
    component: r => require.ensure([], () => r(Home), 'homePage'),
  },
  {
    path: '/user',
    component: r => require.ensure([], () => r(UserMainPage), 'userMainPage'),
    children: [{
      path: '',
      title: '账号列表页面',
      name: 'userList',
      component: r => require.ensure([], () => r(UserList), 'userList'),
    }, {
      path: 'add',
      title: '账号添加',
      name: 'userDetail',
      component: r => require.ensure([], () => r(UserDetail), 'userDetail'),
    }, {
      path: 'edit',
      title: '账号编辑',
      name: 'userDetail',
      component: r => require.ensure([], () => r(UserDetail), 'userDetail'),
    }],
  },
  {
    path: '/group-collection',
    name: 'groupCollection',
    title: '群库管理',
    component: r => require.ensure([], () => r(GroupCollectionManager), 'groupCollection'),
  },
  {
    path: '/count',
    name: 'count',
    title: '统计',
    component: r => require.ensure([], () => r(count), 'count'),
    children: [{
      path: 'countList',
      name: 'countList',
      component: r => require.ensure([], () => r(countList), 'countList'),
    }, {
      path: 'propellingMovementNum',
      name: 'propellingMovementNum',
      component: r => require.ensure([], () => r(propellingMovementNum), 'propellingMovementNum'),
    }, {
      path: 'userPhone',
      name: 'userPhone',
      title: '管理用户',
      component: r => require.ensure([], () => r(userPhoneManage), 'userPhoneManage'),
    }, {
      path: 'dayPlay',
      name: 'dayPlay',
      title: '日活统计',
      component: r => require.ensure([], () => r(dayPlay), 'dayPlay'),
    }, {
      path: 'mouthPlay',
      name: 'mouthPlay',
      title: '月活统计',
      component: r => require.ensure([], () => r(mouthPlay), 'mouthPlay'),
    }, {
      path: 'sevenKeep',
      name: 'sevenKeep',
      title: '7天留存率',
      component: r => require.ensure([], () => r(sevenKeepPercent), 'sevenKeepPercent'),
    }, {
      path: 'blackList',
      name: 'blackList',
      title: '黑名单管理',
      component: r => require.ensure([], () => r(blackList), 'blackList'),
    }, {
      path: 'whiteList',
      name: 'whiteList',
      title: '白名单管理',
      component: r => require.ensure([], () => r(whiteList), 'whiteList'),
    }, {
      path: 'callbackAddress',
      name: 'callbackAddress',
      title: '回调地址配置',
      component: r => require.ensure([], () => r(callbackAddress), 'callbackAddress'),
    }, {
      path: 'blackTacticsList',
      name: 'blackTacticsList',
      title: '回调地址配置',
      component: r => require.ensure([], () => r(blackTacticsList), 'blackTacticsList'),
    }],
  },
  {
    path: '/group/:id',
    name: 'group',
    title: '群管理',
    component: r => require.ensure([], () => r(GroupManager), 'groupManager'),
  },
  {
    path: '/wechat-offical-account',
    name: 'woaManagerMainPage',
    component: r => require.ensure([], () => r(WoaManagerMain), 'woaManagerMainPage'),
    children: [{
      path: '',
      title: '微信公众账号管理页面',
      name: 'woaManagerPage',
      component: r => require.ensure([], () => r(WoaManagerList), 'woaManagerPage'),
    }, {
      path: 'add',
      title: '添加公众号',
      name: 'woaAddPage',
      component: r => require.ensure([], () => r(WoaAddPage), 'woaAddPage'),
    }, {
      path: 'edit/:id',
      title: '编辑公众号',
      name: 'woaEditPage',
      component: r => require.ensure([], () => r(WoaAddPage), 'woaEditPage'),
    }],
  },
  {
    path: '/message',
    title: '消息推送',
    name: 'pushMainPage',
    component: r => require.ensure([], () => r(PushMain), 'pushMainPage'),
    children: [{
      path: 'template',
      name: 'templateMessageMainPage',
      component: r => require.ensure([], () => r(TemplateMain), 'templateMessageMainPage'),
      children: [{
        path: '',
        title: '模版消息管理页面',
        name: 'customerMessagePage',
        component: r => require.ensure([], () => r(TemplateList), 'templateMessagePage'),
      }, {
        path: 'add',
        title: '添加模版消息',
        name: 'customerMessageAddPage',
        props: {
          type: TYPE_IS_ADD,
        },
        component: r => require.ensure([], () => r(TemplateDetail), 'templateMessageAddPage'),
      }, {
        path: 'edit/:id',
        title: '编辑模版消息',
        name: 'customerMessageDetailPage',
        props: (route) => {
          const ret = {
            type: TYPE_IS_EDIT,
            id: route.params.id,
          };
          return ret;
        },
        component: r => require.ensure([], () => r(TemplateDetail), 'templateMessageDetailPage'),
      }],
    }, {
      path: 'customer',
      name: 'customerMessageMainPage',
      component: r => require.ensure([], () => r(CustomerMain), 'customerMessageMainPage'),
      children: [{
        path: '',
        title: '微信客服消息管理页面',
        name: 'customerMessagePage',
        component: r => require.ensure([], () => r(CustomerList), 'customerMessagePage'),
      }, {
        path: 'add',
        title: '添加客服消息',
        name: 'customerMessageAddPage',
        props: {
          type: TYPE_IS_ADD,
        },
        component: r => require.ensure([], () => r(CustomerDetail), 'customerMessageAddPage'),
      }, {
        path: 'edit/:id',
        title: '编辑微信客服消息',
        name: 'customerMessageDetailPage',
        props: (route) => {
          const ret = {
            type: TYPE_IS_EDIT,
            id: route.params.id,
          };
          return ret;
        },
        component: r => require.ensure([], () => r(CustomerDetail), 'customerMessageDetailPage'),
      }],
    }, {
      path: 'subscribe',
      name: 'subscribeMessageMainPage',
      component: r => require.ensure([], () => r(SubscribeMain), 'subscribeMessageMainPage'),
      children: [{
        path: '',
        title: '微信订阅关注消息管理页面',
        name: 'subscribeMessagePage',
        component: r => require.ensure([], () => r(SubscribeList), 'subscribeMessagePage'),
      }, {
        path: 'add',
        title: '添加订阅消息',
        name: 'subscribeMessageAddPage',
        props: {
          type: TYPE_IS_ADD,
        },
        component: r => require.ensure([], () => r(SubscribeDetail), 'subscribeMessageAddPage'),
      }, {
        path: 'edit/:id',
        title: '编辑微信订阅消息',
        name: 'subscribeMessageDetailPage',
        props: (route) => {
          const ret = {
            type: TYPE_IS_EDIT,
            id: route.params.id,
          };
          return ret;
        },
        component: r => require.ensure([], () => r(SubscribeDetail), 'subscribeMessageDetailPage'),
      }],
    }],
  },
];

Vue.use(VueRouter);

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  console.log('router changed. ', to.name, to.path);
  const toPath = to.path;
  if (!auth.getToken() && pulicRoutePaths.indexOf(toPath) === -1) {
    alert('您尚未登录，请重新登录');
    router.replace('/');
    return;
  }
  next();
});
export default router;
